home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / tutor / pro15 / rand01.bas < prev    next >
Encoding:
BASIC Source File  |  1991-02-02  |  2.7 KB  |  74 lines

  1. 10 'RAND01.BAS - a random file that contains the following:
  2. 20 'FIRSTNAME$   The first name of the person on file
  3. 30 'LASTNAME$    The last name of the person on file
  4. 40 'SOCSEC$      The social security number of the person on file
  5. 50 '
  6. 60 'First, set up the fields.  We'll use a total record length of 64, and
  7. 70 'divide it up as follows:
  8. 80 '
  9. 90 'FIRSTNAME$ length will be 23
  10. 100 'LASTNAME$ length will be 30
  11. 110 'SOCSEC$   length will be 11 (total = 64)
  12. 120 '
  13. 130 'First step:  open the file and set up the fields...
  14. 140 '
  15. 150 OPEN "EMPLOYEE.DAT" FOR RANDOM AS #1 LEN=64
  16. 160 FIELD #1,23 AS FIRSTNAME$,30 AS LASTNAME$,11 AS SOCSEC$
  17. 170 '
  18. 180 'Second step:  determine the end of file.  If this is the first time that
  19. 190 'you've entered information, then it will be zero.  Otherwise, it will be
  20. 200 'the record number of the last number...
  21. 210 '
  22. 220 LASTRECORD=LOF(1)/64
  23. 230 '
  24. 240 'Note we've called the variable that will contain the last record number as
  25. 250 'LASTRECORD (an easy name to remember), that we used LOF(1) since this is
  26. 260 'the first file (#1) we've opened, and divided by 64 since this is what we
  27. 270 'decided to use for a record length.
  28. 280 '
  29. 290 'Now, let's get some information to write to the disk file.  We'll ask the
  30. 300 'user if he/she wants to enter information, if the answer is 'Y' then we'll
  31. 310 'enter the information.  If the answer is 'N' then we'll display all the
  32. 320 'entries currently on disk...
  33. 330 '
  34. 340 KEY OFF:CLS
  35. 350 INPUT "Do you want to enter a record?  Answer Y for Yes, N for No";ANSWER$
  36. 360 IF ANSWER$="Y" OR ANSWER$="y" THEN GOTO 500
  37. 370 '
  38. 380 'No more records, so show all entries...
  39. 390 '
  40. 400 CLS
  41. 410 '
  42. 420 'Here's how easy it is to get information WITHOUT closing the file first!
  43. 430 '
  44. 440 FOR RECORD = 1 TO LASTRECORD
  45. 450 GET #1,RECORD
  46. 460 PRINT USING "\                       \ \                    \ \         \";FIRSTNAME$;LASTNAME$;SOCSEC$
  47. 470 NEXT RECORD
  48. 480 CLOSE #1
  49. 490 END
  50. 500 '
  51. 510 'User decided to put in records, so let's do so...
  52. 520 INPUT "What is the FIRST NAME";FIRNAME$
  53. 530 PRINT
  54. 540 INPUT "What is the LAST NAME";LASNAME$
  55. 550 PRINT
  56. 560 PRINT "(for the SSN, use this format    => ###-##-####"
  57. 570 INPUT "What is the SOCIAL SECURITY NUMBER";SNUMBER$
  58. 580 '
  59. 590 'We now have the needed information, so let's put it on disk...
  60. 600 '
  61. 610 LSET FIRSTNAME$=FIRNAME$
  62. 620 LSET LASTNAME$=LASNAME$
  63. 630 LSET SOCSEC$=SNUMBER$
  64. 640 '
  65. 650 'Now, add one to the LASTRECORD so this record is saved after it...
  66. 660 LASTRECORD=LASTRECORD+1
  67. 670 PUT #1,LASTRECORD
  68. 680 '
  69. 690 'Record stored safely on disk, now see if there are any more to put in...
  70. 700 GOTO 340
  71. 710 END
  72. 720 'End of program - RAND01.BAS
  73. 730 'From the GW-BASIC TUTORIAL SERIES (GWBT08, 02/01/1991)
  74.